//
//  TimerManager.m
//  UUZuche
//
//  Created by wangzhuoxuan on 14/10/21.
//
//

#import "TimerManager.h"


@interface TimerManager ()

@property (nonatomic, strong) NSTimer *timer;

@end


@implementation TimerManager

+ (instancetype)sharedInstance
{
    static TimerManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:_sharedInstance
                                                 selector:@selector(logoutToInvalidate:)
                                                     name:@"DID_FINISH_LOGOUT"
                                                   object:nil];
    });

    return _sharedInstance;
}

- (void)startTimerWithTotalInterval:(NSInteger)total
                     passedInterval:(NSInteger)passed
                            handler:(TimerHandler)handler
{
    if ((total - passed) <= 0)
    {
        [self invalidateTimer];
        return;
    }

    if (self.timer)
    {
        [self invalidateTimer];
    }

    self.totalInterval = total;
    self.passedInterval = passed;
    self.timerHandler = handler;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(processTimer)
                                                userInfo:nil
                                                 repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)invalidateTimer
{
    [self.timer invalidate];
    self.timer = nil;
    self.timerHandler = nil;
    self.totalInterval = 0;
    self.passedInterval = 0;
    self.process = 0.0;
}

- (void)processTimer
{
    self.passedInterval += 1;
    self.process = (float)self.passedInterval / self.totalInterval;

    NSInteger interval = self.totalInterval - self.passedInterval;
    if (self.timerHandler)
    {
        self.timerHandler(interval);
    }

//    if (self.passedInterval % 60 == 0) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"COUNTDOWN_TIMER_PASS_MINUTE"
//                                                            object:nil
//                                                          userInfo:nil];
//    }
//    
//    if (interval <= 0)
//    {
//        [self invalidateTimer];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"COUNTDOWN_TIMER_TIMEOUT"
//                                                            object:nil
//                                                          userInfo:nil];
//    }
}

- (void)logoutToInvalidate:(NSNotification *)notification
{
    [self invalidateTimer];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"COUNTDOWN_TIMER_TIMEOUT"
//                                                        object:nil
//                                                      userInfo:nil];
}
@end
